package com.tianxia120.website.admin.controller;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.paragetter.Para;
import com.tianxia120.website.admin.service.ArticleService;
import com.tianxia120.website.bean.model.Article;
import com.tianxia120.website.data.annotation.IgnoreToken;
import com.tianxia120.website.data.event.Test1Event;
import com.tianxia120.website.data.controller.BaseController;
import com.tianxia120.website.data.interceptors.Delete;
import com.tianxia120.website.data.interceptors.Get;
import com.tianxia120.website.data.interceptors.Post;
import com.tianxia120.website.data.utils.RongYunSysMessage;
import net.dreamlu.event.EventKit;

import java.util.ArrayList;
import java.util.List;

/**
 * @createTime: 2018/9/21 14:44
 * @author: HingLo
 * @description: 文章管理
 */
public class ArticleController extends BaseController {
    @Inject
    private ArticleService articleService;

    /**
     * 通过文章的编号来获取文章具体内容
     *
     * @param id 文章的ID
     */
    @IgnoreToken
    @Before(Get.class)
    public void index(Long id) {
        if (id == null) {
            error("文章ID不能为空");
        } else {
            renderJson(articleService.indexService(id));
        }

    }

    /**
     * 添加文章内容
     *
     * @param article 文章对象
     */
    @Before(Post.class)
    public void add(@Para("") Article article) {
        renderJson(articleService.addService(article));
    }

    /**
     * 删除指定的文章
     *
     * @param id 文章的ID
     */
    @Before(Delete.class)
    public void delete(Long id) {
        if (id == null || id < 1) {
            error("文章ID不正确");
        }
        result(articleService.deleteService(id));
    }

    /**
     * 前端启用状态的文章
     *
     */
    @Before(Get.class)
    @IgnoreToken
    public void list() {
        success(articleService.listService(getPage(), getSize(), 1));
    }

    /**
     * 后台管理查看全部文章
     */
    @Before(Get.class)
    public void listAll() {
        success(articleService.listService(getPage(), getSize(), 0));
    }

    /**
     * 事件机制 测试
     *
     * @param id 随便
     */
    @IgnoreToken
    @Before(Get.class)
    public void envent(Long id) {

        RongYunSysMessage rongYunSysMessage = new RongYunSysMessage();
        rongYunSysMessage.setMessage("这是我的要发送的消息");
        rongYunSysMessage.setExtraType("额外的消息");
        List<String> list = new ArrayList<>();
        list.add("1");
        rongYunSysMessage.setUserId(list);
        Long start = System.currentTimeMillis();
        EventKit.post(new Test1Event(rongYunSysMessage));
        System.out.println("事件耗费时间:" + (System.currentTimeMillis() - start));
        success();

    }


}
